package bookshop.service.impl;

import java.util.List;

import bookshop.dao.BookDao;
import bookshop.dao.CustCommentDao;
import bookshop.dao.CustomerDao;
import bookshop.dao.DaoFactory;
import bookshop.domain.Book;
import bookshop.domain.CustComment;
import bookshop.domain.Customer;
import bookshop.service.CustCommentService;
import bookshop.service.helper.CustCommentQueryHelper;
import bookshop.utils.Page;

public class CustCommentServiceImpl implements CustCommentService {

	public void saveCustComment(CustComment comment) {
		CustCommentDao custCommentDao = (CustCommentDao) DaoFactory
				.getDao("custCommentDao");
		//添加用户评论信息
		custCommentDao.AddCustCommet(comment);
		
		Customer customer = new Customer();
		Long count = 0L;
        //更新用户的评述等级		
		count = custCommentDao.countCommentByCustId(comment.getCustomer().getCustomerId());
		CustomerDao customerDao = (CustomerDao)DaoFactory.getDao("customerDao");
		customerDao.updateMoodLevelByCount(count, comment.getCustomer().getCustomerId());
		
		//更新书本的等级分数
	    int mark = custCommentDao.avgMarkByBookCode(comment.getBook().getBookCode());
		BookDao bookDao = (BookDao)DaoFactory.getDao("bookDao");
		Book book = new Book();
		book = bookDao.getBookByCode(comment.getBook().getBookCode());
		book.setBookImage(bookDao.getBookCover(comment.getBook().getBookCode()));
		book.setBookMark(mark);
		bookDao.update(book);
	}
	
	
	public Page loadCommentsByCondition(CustCommentQueryHelper helper, Page page){
		CustCommentDao custCommentDao = (CustCommentDao)DaoFactory.getDao("custCommentDao");
		page.setTotalRecNum(custCommentDao.countCommentsByCondition(helper));
		page.setPageContent(custCommentDao.getScopedComments(helper, page.getStartIndex(), page.getPageSize()));
		
		return page;
	}

	
	public void delCustCommentById(Integer id){
		CustCommentDao custCommentDao = (CustCommentDao) DaoFactory
				.getDao("custCommentDao");
		
		CustComment custComment = custCommentDao.getCustCommentById(id);
		//删除用户评论
		custCommentDao.delCustCommentById(id);
		
		Customer customer = new Customer();
		Long count = 0L;
		//更新用户的评述等级
		count = custCommentDao.countCommentByCustId(custComment.getCustomer().getCustomerId());
		CustomerDao customerDao = (CustomerDao)DaoFactory.getDao("customerDao");
		customerDao.updateMoodLevelByCount(count, custComment.getCustomer().getCustomerId());
		
		//更新书本的等级分数
	    int mark = custCommentDao.avgMarkByBookCode(custComment.getBook().getBookCode());
		BookDao bookDao = (BookDao)DaoFactory.getDao("bookDao");
		Book book = new Book();
		book = bookDao.getBookByCode(custComment.getBook().getBookCode());
		book.setBookImage(bookDao.getBookCover(custComment.getBook().getBookCode()));
		book.setBookMark(mark);
		bookDao.update(book);
	}

	public Page loadCommentsByCustId(CustCommentQueryHelper helper, Page page) {
		CustCommentDao custCommentDao = (CustCommentDao)DaoFactory.getDao("custCommentDao");
		page.setTotalRecNum(custCommentDao.countCommentsByCondition(helper));
		page.setPageContent(custCommentDao.getScopedComments(helper, page.getStartIndex(), page.getPageSize()));
		return page;
	}


	


}
